package com.playcrab.adouban;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.douban.ReviewEntry;
import com.google.gdata.data.douban.ReviewFeed;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.util.ServiceException;
import com.playcrab.adouban.util.DoubanAPI;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public abstract class ViewSubject extends BaseActivity {
	protected SubjectEntry subject; 
	protected Handler handler = new Handler();
	protected String id = null;
	protected String type = null;
	private static String LOG_TAG = "ViewSubject"; 
	private static final int MENU_SUBJECT_VIEW_DOUBAN = Menu.FIRST + 1000;
	private static final int MENU_SUBJECT_VIEW_MDOUBAN = Menu.FIRST + 1001; 
	private static final int MENU_SUBJECT_COLLECTION = Menu.FIRST + 1002;
	private static final int MENU_VIEW_MDOUBAN = Menu.FIRST + 101;
	private static final int MENU_VIEW_DOUBAN = Menu.FIRST + 102;
	abstract public SubjectEntry getSubject()  throws IOException, ServiceException;
	abstract public void showSubject();
	private ListView reviewList;
	ReviewFeed reviewFeed;
	List<ReviewEntry> reviews;
	protected View subjectHeader;
	
	
	
	public TextView newAttributeView(Map<String, List<String>>  attributes, String attrName, String label){
		TextView view = new TextView(this); 
		if(attributes.get(attrName)!=null){
			String values= DoubanAPI.getInstance().joinList(attributes.get(attrName)," / ");  
			view.setText(label.concat(values) );
			
		}  
		return view; 
	}
	public TextView newRatingView(){
		TextView  rating = new TextView(this); 
		rating.setText("评分: ".concat(subject.getRating().getAverage().toString())
				.concat(" (共").concat( subject.getRating().getNumRaters().toString()).concat("人评价)") );
		return rating;
		
	}
	
	public static Class getViewClass(String type){
		   if(type.equalsIgnoreCase("book"))
			   return   ViewBook.class;
		   else  if(type.equalsIgnoreCase("movie"))
			   return   ViewMovie.class;
		   else if(type.equalsIgnoreCase("music"))
			   return   ViewMusic.class;
		   else
			   return null;
      }
	 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_SUBJECT_COLLECTION, 0, "收藏").setIcon(R.drawable.bookmark)
		.setAlphabeticShortcut('B');
		menu.add(0, MENU_SUBJECT_VIEW_DOUBAN, 0, "豆瓣").setIcon(R.drawable.search)
				.setAlphabeticShortcut('D');
		menu.add(0, MENU_SUBJECT_VIEW_MDOUBAN, 0, "手机豆瓣").setIcon(R.drawable.mobile)
				.setAlphabeticShortcut('M'); 
		 
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_SUBJECT_COLLECTION: { 
				Intent intent = new Intent(this, EditCollection.class); 
				intent.putExtra("type", type);
				intent.putExtra("id", this.subject.getId() );  
				intent.putExtra("title", this.subject.getTitle().getPlainText());
				startActivity(intent);  
				return true;
			}
			case MENU_SUBJECT_VIEW_DOUBAN: {
				String url = "http://www.douban.com/subject/".concat(id).concat("/"); 
				Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
				startActivity(openBrowserIntent);  
				return true;
			}
			case MENU_SUBJECT_VIEW_MDOUBAN:{
				String url = "http://m.douban.com/".concat(type).concat("/subject/").concat(this.id).concat("/"); 
				Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
				startActivity(openBrowserIntent);
				return true;
			}
//			case MENU_SUBJECT_SHARE:{
//			       
//			}	

		}
		return super.onOptionsItemSelected(item);
	}
	
	public void showReview() {
		setContentView(R.layout.view_subject);
		reviewList = (ListView) findViewById(R.id.reviews);
		registerForContextMenu(reviewList);
		
		reviews = reviewFeed.getEntries();
		if(subjectHeader != null){
			TextView view = new TextView(this);
			view.setText("This is the very header here!");
			reviewList.addHeaderView(subjectHeader);
		}
		reviewList.setAdapter(new ReviewListAdapter(
				ViewSubject.this, reviews));
	}
	
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("操作菜单");
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		menu.add(0, MENU_VIEW_MDOUBAN, 0, "在手机豆瓣中查看完整评论");
		menu.add(0, MENU_VIEW_DOUBAN, 0, "在豆瓣网中查看完整评论");

	}
	
    @Override 
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		ReviewEntry re = reviews.get(info.position - 1);
		String review_id = re.getId().substring(re.getId().lastIndexOf('/')+1); 
        switch (item.getItemId()) {
			case MENU_VIEW_MDOUBAN:{
				String url = "http://m.douban.com/".concat(type).concat("/review/").concat(review_id).concat("/"); 
				Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
				startActivity(openBrowserIntent);  
				return true;			  
			}
			case MENU_VIEW_DOUBAN:{
				String url = "http://www.douban.com/review/".concat(review_id).concat("/"); 
				Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
				startActivity(openBrowserIntent);  
				return true;				  
			}
        }
        
		return super.onOptionsItemSelected(item);
        
    }
}
